package okhttp3;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v4.view.LayoutInflaterFactory;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.TintContextWrapper;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class SSAppCompatViewInflater {
    class DeclaredOnClickListener implements View.OnClickListener {
        private final View mHostView;
        private final String mMethodName;
        private Context mResolvedContext;
        private Method mResolvedMethod;

        public DeclaredOnClickListener(@NonNull View arg1, @NonNull String arg2) {

            this.mHostView = arg1;
            this.mMethodName = arg2;
        }

        public void onClick(@NonNull View arg5) {
            if (this.mResolvedMethod == null) {
                this.resolveMethod(this.mHostView.getContext(), this.mMethodName);
            }

            try {
                this.mResolvedMethod.invoke(this.mResolvedContext, arg5);
                return;
            } catch (InvocationTargetException v0) {
                throw new IllegalStateException("Could not execute method for android:onClick", ((Throwable) v0));
            } catch (IllegalAccessException v0_1) {
                throw new IllegalStateException("Could not execute non-public method for android:onClick", ((Throwable) v0_1));
            }
        }

        @NonNull
        private void resolveMethod(@Nullable Context arg7, @NonNull String arg8) {
            Context v0;
            for (v0 = arg7; v0 != null; ) {
                try {
                    if (v0.isRestricted()) {
                    } else {
                        Method v1_1 = v0.getClass().getMethod(this.mMethodName, View.class);
                        if (v1_1 == null) {
                        } else {
                            this.mResolvedMethod = v1_1;
                            this.mResolvedContext = v0;
                            return;
                        }
                    }
                } catch (NoSuchMethodException v1) {
                }
                if ((v0 instanceof ContextWrapper)) {
                    v0 = ((ContextWrapper) v0).getBaseContext();
                    continue;
                }
            }

            int v0_1 = this.mHostView.getId();
            String v0_2 = v0_1 == -1 ? "" : " with id \'" + this.mHostView.getContext().getResources().getResourceEntryName(v0_1) + "\'";
            throw new IllegalStateException("Could not find method " + this.mMethodName
                    + "(View) in a parent or ancestor Context for android:onClick attribute defined on view " + this.mHostView.getClass() + v0_2);
        }
    }

    private static final String LOG_TAG = "SSAppCompatViewInflater";
    private final Object[] mConstructorArgs;
    private static final String[] sClassPrefixList = new String[]{"android.widget.", "android.view.", "android.webkit."};
    private static final Map sConstructorMap = new ArrayMap();
    private static final Class[] sConstructorSignature = new Class[]{Context.class, AttributeSet.class};
    private static final int[] sOnClickAttrs = new int[]{16843375};

    public SSAppCompatViewInflater() {
        this.mConstructorArgs = new Object[2];
    }

    private void checkOnClickListener(View arg4, AttributeSet arg5) {
        Context v0 = arg4.getContext();
        if (((v0 instanceof ContextWrapper)) && (Build.VERSION.SDK_INT < 15 || (ViewCompat.hasOnClickListeners(arg4)))) {
            TypedArray v0_1 = v0.obtainStyledAttributes(arg5, sOnClickAttrs);
            String v1 = v0_1.getString(0);
            if (v1 != null) {
                arg4.setOnClickListener(new DeclaredOnClickListener(arg4, v1));
            }

            v0_1.recycle();
        }
    }

    public final View createView(View arg5, String arg6, @NonNull Context arg7, @NonNull AttributeSet arg8, boolean arg9, boolean arg10, boolean arg11, boolean arg12) {
        View v0_1;
        Context v0 = !arg9 || arg5 == null ? arg7 : arg5.getContext();
        if ((arg10) || (arg11)) {
            v0 = themifyContext(v0, arg8, arg10, arg11);
        }

        if (arg12) {
            v0 = TintContextWrapper.wrap(v0);
        }

        AppCompatImageView v1 = null;
        int v2 = -1;
        switch (arg6.hashCode()) {
            case 1125864064: {
                if (arg6.equals("ImageView")) {
                    v2 = 0;
                }

                break;
            }
        }

        switch (v2) {
            case 0: {
                v1 = new AppCompatImageView(v0, arg8);
                break;
            }
        }

        if (v1 != null || arg7 == v0) {
            v0_1 = v1;
        } else {
            v0_1 = this.createViewFromTag(v0, arg6, arg8);
        }

        if (v0_1 != null) {
            this.checkOnClickListener(v0_1, arg8);
        }

        return v0_1;
    }

    private View createView(Context arg3, String arg4, String arg5) throws ClassNotFoundException, InflateException {
        String v0_2;
        ClassLoader v1;
        Object v0 = sConstructorMap.get(arg4);
        if (v0 == null) {
            try {
                v1 = arg3.getClassLoader();
                if (arg5 != null) {
                    v0_2 = arg5 + arg4;
                } else {
                    v0_2 = arg4;
                }
                Constructor v0_3 = v1.loadClass(v0_2).asSubclass(View.class).getConstructor(sConstructorSignature);
                sConstructorMap.put(arg4, v0_3);
            } catch (Exception v0_1) {
                v0 = null;
            }
        }
        try {
            ((Constructor) v0).setAccessible(true);
            v0 = ((Constructor) v0).newInstance(this.mConstructorArgs);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return ((View) v0);
    }

    private View createViewFromTag(Context arg6, String arg7, AttributeSet arg8) {
        View v0_2;
        int v2;
        String v1 = null;
        if (arg7.equals("view")) {
            arg7 = arg8.getAttributeValue(v1, "class");
        }

        this.mConstructorArgs[0] = arg6;
        this.mConstructorArgs[1] = arg8;
        if (-1 != arg7.indexOf(46)) {
            String v0_3 = null;
            try {
                v0_2 = this.createView(arg6, arg7, v0_3);
            } catch (Exception v0_1) {
                label_49:
                this.mConstructorArgs[0] = v1;
                this.mConstructorArgs[1] = v1;
                return null;
            }

            this.mConstructorArgs[0] = v1;
            this.mConstructorArgs[1] = v1;
            return v0_2;
        }

        v2 = 0;
        try {
            while (true) {
                if (v2 >= sClassPrefixList.length) {
                    this.mConstructorArgs[0] = null;
                    this.mConstructorArgs[1] = null;
                    return null;
                }


                v0_2 = this.createView(arg6, arg7, sClassPrefixList[v2]);

                if (v0_2 == null) {
                    ++v2;
                    continue;
                }
                this.mConstructorArgs[0] = null;
                this.mConstructorArgs[1] = null;
                return v0_2;
            }
        } catch (ClassNotFoundException e) {

        }
        return null;
    }

    public static LayoutInflater from(Context arg2) {
        LayoutInflater v0;
        if (arg2 == null) {
            v0 = null;
        } else {
            v0 = LayoutInflater.from(arg2);
            if (v0.getFactory() == null) {
                LayoutInflaterCompat.setFactory(v0, new LayoutInflaterFactory() {
                    final boolean isPre21 = Build.VERSION.SDK_INT < 21 ? true : false;

                    private SSAppCompatViewInflater mAppCompatViewInflater;

                    public final View onCreateView(View arg10, String arg11, Context arg12, AttributeSet arg13) {
                        if (this.mAppCompatViewInflater == null) {
                            this.mAppCompatViewInflater = new SSAppCompatViewInflater();
                        }

                        return this.mAppCompatViewInflater.createView(arg10, arg11, arg12, arg13, this.isPre21, this.isPre21, true, true);
                    }
                });
            }
        }

        return v0;
    }

    private static Context themifyContext(Context arg0, AttributeSet arg1, boolean arg2, boolean arg3) {
        return arg0;
    }
}

